package cn.he4966.caiji;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;

public class IcpClass {


    public String icp;
    public String icp_url;
    public String icp_name;
    public String icp_type;
    public String icp_time;
    public String icp_status;
    public String icp_remark;
    public String icp_remark_url;

    public  String getIcpListDetailUrl(String url) throws IOException {
        this.sleep();
        Document doc = null;
        doc = Jsoup.connect(url).followRedirects(true).get();

        //获取<a class="detail-l-i-right-box-list-detail" href="cd-9536b94f6d68aee19f58852c77e34801.html" target="_blank">企业详情</a> 里面的 href值
        return doc.select("a[class=detail-l-i-right-box-list-detail]").attr("href");

//        return doc.body().html();
    }


    public  String getIcpCd(String url) throws IOException {

        //延迟3-9秒再执行
        this.sleep();

        Document doc = null;
        doc = Jsoup.connect(url).followRedirects(true).get();
        //获取<input type="hidden" id="company_name_digest" value="9536b94f6d68aee19f58852c77e34801"> 里面的value
        String digest = doc.select("input[id=company_name_digest]").attr("value");
        if(digest.length()>6){
        //Jsoup表单数据page=1&limit=999&digest=9536b94f6d68aee19f58852c77e34801 post提交到https://www.aichaicp.com/api/company?action=get_link_list_v2
            String post = "page=1&limit=999&digest="+digest;
            String json = Jsoup.connect("https://www.aichaicp.com/api/company?action=get_link_list_v2")
                    .header("Content-Type", "application/x-www-form-urlencoded")
                    .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")
                    .requestBody(post)
                    .ignoreContentType(true)
                    .post()
                    .body().text();

            //解析json字符串为json对象
            //{"status":0,"message":"\u8bf7\u6c42\u6210\u529f!","data":{"mobile_cnt":6,"tel_cnt":0,"email_cnt":3,"address_cnt":5,"mobile_rows":[{"type":"phone","tag":"\u8d6b","link_str":"139********","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["20*****","*******","*******","*******"],"phone_tag":"normal","phone_infos":{"find":true,"phone":"13906181580","province":"\u6c5f\u82cf","city":"\u65e0\u9521","phone_type":"\u79fb\u52a8"}},{"type":"phone","tag":"\u8d6b","link_str":"137********","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["20*****","*******","*******"],"phone_tag":"normal","phone_infos":{"find":true,"phone":"13771082690","province":"\u6c5f\u82cf","city":"\u65e0\u9521","phone_type":"\u79fb\u52a8"}},{"type":"phone","tag":"\u8d6b","link_str":"181********","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["20*****"],"phone_tag":"normal","phone_infos":{"find":true,"phone":"18118907182","province":"\u6c5f\u82cf","city":"\u65e0\u9521","phone_type":"\u7535\u4fe1"}},{"type":"phone","tag":"\u8d6b","link_str":"137********","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["20*****","***"],"phone_tag":"normal","phone_infos":{"find":true,"phone":"13771076305","province":"\u6c5f\u82cf","city":"\u65e0\u9521","phone_type":"\u79fb\u52a8"}},{"type":"phone","tag":"\u8d6b","link_str":"186********","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["20*****"],"phone_tag":"normal","phone_infos":{"find":true,"phone":"18605108340","province":"\u6c5f\u82cf","city":"\u65e0\u9521","phone_type":"\u8054\u901a"}},{"type":"phone","tag":"\u8d6b","link_str":"181********","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["\u4e92\u8054*"],"phone_tag":"normal","phone_infos":{"find":true,"phone":"18168908338","province":"\u6c5f\u82cf","city":"\u65e0\u9521","phone_type":"\u7535\u4fe1"}}],"tel_rows":[],"email_rows":[{"type":"email","tag":"\u8d6b","link_str":"28336****@qq.com","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["20*****","*******","*******","*******","*******","*******"]},{"type":"email","tag":"\u8d6b","link_str":"28336****@Qq.com","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["20*****"]},{"type":"email","tag":"\u8d6b","link_str":"329065****@qq.com","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["20*****"]}],"address_rows":[{"type":"address","tag":"\u8d6b","link_str":"\u65e0\u9521\u5e02\u65b0\u5434\u533a*****02","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["20*****","*******","*******","*******","*******"]},{"type":"address","tag":"\u8d6b","link_str":"\u65e0\u9521\u5e02\u946b\u5929\u4e1a****\u516c\u53f8","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["20*****"]},{"type":"address","tag":"\u8d6b","link_str":"\u65e0\u9521\u5e02\u60e0\u5c71\u533a*****1\u5ba4","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["20*****","*******"]},{"type":"address","tag":"\u8d6b","link_str":"\u65e0\u9521\u5e02\u60e0\u5c71\u533a*****\u533a)","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["20*****","*******"]},{"type":"address","tag":"\u8d6b","link_str":"\u5b9c\u5174\u5e02\u82b3\u6865\u8857*****6\u53f7","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["\u5de5\u5546**"]}],"total_rows":[{"type":"phone","tag":"\u8d6b","link_str":"139********","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["20*****","*******","*******","*******"],"phone_tag":"normal","phone_infos":{"find":true,"phone":"13906181580","province":"\u6c5f\u82cf","city":"\u65e0\u9521","phone_type":"\u79fb\u52a8"}},{"type":"phone","tag":"\u8d6b","link_str":"137********","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["20*****","*******","*******"],"phone_tag":"normal","phone_infos":{"find":true,"phone":"13771082690","province":"\u6c5f\u82cf","city":"\u65e0\u9521","phone_type":"\u79fb\u52a8"}},{"type":"phone","tag":"\u8d6b","link_str":"181********","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["20*****"],"phone_tag":"normal","phone_infos":{"find":true,"phone":"18118907182","province":"\u6c5f\u82cf","city":"\u65e0\u9521","phone_type":"\u7535\u4fe1"}},{"type":"phone","tag":"\u8d6b","link_str":"137********","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["20*****","***"],"phone_tag":"normal","phone_infos":{"find":true,"phone":"13771076305","province":"\u6c5f\u82cf","city":"\u65e0\u9521","phone_type":"\u79fb\u52a8"}},{"type":"phone","tag":"\u8d6b","link_str":"186********","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["20*****"],"phone_tag":"normal","phone_infos":{"find":true,"phone":"18605108340","province":"\u6c5f\u82cf","city":"\u65e0\u9521","phone_type":"\u8054\u901a"}},{"type":"phone","tag":"\u8d6b","link_str":"181********","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["\u4e92\u8054*"],"phone_tag":"normal","phone_infos":{"find":true,"phone":"18168908338","province":"\u6c5f\u82cf","city":"\u65e0\u9521","phone_type":"\u7535\u4fe1"}},{"type":"email","tag":"\u8d6b","link_str":"28336****@qq.com","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["20*****","*******","*******","*******","*******","*******"]},{"type":"email","tag":"\u8d6b","link_str":"28336****@Qq.com","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["20*****"]},{"type":"email","tag":"\u8d6b","link_str":"329065****@qq.com","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["20*****"]},{"type":"address","tag":"\u8d6b","link_str":"\u65e0\u9521\u5e02\u65b0\u5434\u533a*****02","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["20*****","*******","*******","*******","*******"]},{"type":"address","tag":"\u8d6b","link_str":"\u65e0\u9521\u5e02\u946b\u5929\u4e1a****\u516c\u53f8","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["20*****"]},{"type":"address","tag":"\u8d6b","link_str":"\u65e0\u9521\u5e02\u60e0\u5c71\u533a*****1\u5ba4","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["20*****","*******"]},{"type":"address","tag":"\u8d6b","link_str":"\u65e0\u9521\u5e02\u60e0\u5c71\u533a*****\u533a)","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["20*****","*******"]},{"type":"address","tag":"\u8d6b","link_str":"\u5b9c\u5174\u5e02\u82b3\u6865\u8857*****6\u53f7","kp":false,"link_person":"","link_person_tag":"","link_person_digest":"9536b94f6d68aee19f58852c77e34801","source":["\u5de5\u5546**"]}],"total_cnt":14}}


            return json;
        }
        return "";


    }


    protected void sleep(int second) {
        try {
            if(second>0){
                Thread.sleep(second*1000);

            }else{
                second = 3000 + (int) (Math.random() * 6000);
                Thread.sleep(second);
                System.out.println("随机延迟"+second+"秒");
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    protected void sleep() {
        int second;
        try {
                second = 3000 + (int) (Math.random() * 6000);
                Thread.sleep(second);
                System.out.println("随机延迟"+second+"秒");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
